package com.nsgf.library.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.nsgf.library.ui.product.ProduceListActivity;
import com.zhht.htzx.BR;
import com.nsgf.library.data.bean.Category;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.adapter.PrimaryCategoryAdapter;
import com.nsgf.library.ui.adapter.SecondaryCategoryAdapter;
import com.zhht.htzx.R;
import com.nsgf.library.ui.page.BaseActivity;
import com.nsgf.library.ui.page.StateHolder;
import com.nsgf.library.ui.product.ProductDetailActivity;
import com.nsgf.library.utils.StickyHeaderItemDecoration;
import com.zhht.htzx.databinding.ActivityCategoryBinding;

import java.util.ArrayList;
import java.util.List;

public class CategoryActivity extends BaseActivity implements
        PrimaryCategoryAdapter.OnPrimaryCategoryClickListener,
        SecondaryCategoryAdapter.OnSecondaryCategoryClickListener,
        SecondaryCategoryAdapter.OnScrollListener {

    private RecyclerView primaryCategoryRecyclerView;
    private RecyclerView secondaryCategoryRecyclerView;
    private PrimaryCategoryAdapter primaryAdapter;
    private SecondaryCategoryAdapter secondaryAdapter;
    private List<Category> categories = new ArrayList<>();
    private boolean isScrolling = false;

    private StateHolder mStates;

    private AccountRequester mAccountRequester;

    private ActivityCategoryBinding mBinding;

    @Override
    protected void initViewModel() {
        mStates = new StateHolder();
        mAccountRequester = new AccountRequester();
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.activity_category, BR.vm, mStates);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mStates.titleBar.set("商品分类");
        mBinding = (ActivityCategoryBinding) getBinding();
        // 初始化RecyclerView
        primaryCategoryRecyclerView = findViewById(R.id.primary_category_recycler_view);
        secondaryCategoryRecyclerView = findViewById(R.id.secondary_category_recycler_view);

        primaryCategoryRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        secondaryCategoryRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        // 添加粘性头部装饰器
        secondaryCategoryRecyclerView.addItemDecoration(new StickyHeaderItemDecoration());

        // 初始化数据
        initData();

        mBinding.etTypeSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    // 在这里执行搜索操作
                    startActivity(new Intent()
                            .putExtra("searchName", mBinding.etTypeSearch.getText().toString())
                            .putExtra("position", -1)
                            .setClass(CategoryActivity.this, ProduceListActivity.class));
                    return true; // 消费事件，防止默认行为
                }
                return false; // 不消费事件，允许默认行为
            }
        });
    }

    private void initData() {

        mAccountRequester.getAllProducts();
        mAccountRequester.getCategoryResult().observe(this, new Observer<ResponseStatus<List<Category>>>() {
            @Override
            public void onChanged(ResponseStatus<List<Category>> listResponseStatus) {

                if (listResponseStatus.getResponseCode() == 0) {
                    categories = listResponseStatus.getData();

                    // 设置适配器
                    primaryAdapter = new PrimaryCategoryAdapter(CategoryActivity.this, categories, CategoryActivity.this);

                    secondaryAdapter = new SecondaryCategoryAdapter(CategoryActivity.this, categories, CategoryActivity.this, CategoryActivity.this);

                    primaryCategoryRecyclerView.setAdapter(primaryAdapter);
                    secondaryCategoryRecyclerView.setAdapter(secondaryAdapter);
                }

            }
        });

    }

    @Override
    public void onPrimaryCategoryClick(Category category, int position) {
        if (!isScrolling) {
            secondaryCategoryRecyclerView.smoothScrollToPosition(position);
        }
    }

    @Override
    public void onSecondaryCategoryClick(Category category) {
        // TODO: 跳转到对应的商品列表页面
        startActivity(new Intent()
                .putExtra("id", category.getId())
                .setClass(this, ProductDetailActivity.class));

    }

    @Override
    public void onScroll(int position) {
        if (!isScrolling) {
            isScrolling = true;
            primaryAdapter.setSelectedPosition(position);
            primaryCategoryRecyclerView.smoothScrollToPosition(position);
            isScrolling = false;
        }
    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }
} 